<?php

namespace App\Api\Logic;

use App\Api\Logic\Validate\CommonValidate;
use App\Api\Logic\Validate\UserValidate;
use App\Exceptions\ValidationException;
use App\Helpers\Lib\AliSmsServices;
use App\Helpers\ResponseEnum;
use Illuminate\Support\Facades\Cache;

class CommonLogic extends BaseLogic
{
    /**
     * 文件上传
     * @param array $data
     * @return void
     * @throws ValidationException
     */
    public function uploadImg(array $data)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('uploadImg')->check($data)){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,$validate->getError());
        }
    }

    /**
     * 获取实力对象
     * @return CommonValidate
     */
    private function getValidate():CommonValidate{
        return  new CommonValidate();
    }

    /**
     * 发送短信验证码
     * @return void
     */
    public function sendSms($data){
        $validate = $this->getValidate();
        if(!$validate->scene('smsCode')->check($data)){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,$validate->getError());
        }
        //判断用户是否发送过短信
        if(Cache::has(AliSmsServices::$prix.$data['phone'])){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'短信已发送请勿重复发送');
        }
    }
}
